/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package XML;

import db.ConnectionProperties;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 *
 * @author abracadabuda
 */
public class XmlConnections {

    private static final String fileName = "config\\connections.xml"; //connections file
    private Element root;
    private Document document;

    private void openXml() { //open XML file, or create it
        try {
            SAXReader reader = new SAXReader();
            document = reader.read(fileName);
            root = document.getRootElement();
        } catch (DocumentException ex) {
            document = DocumentHelper.createDocument();
            root = document.addElement("Connections");
        }
    }

    private void saveXML() {//save XML file
        try {
            FileWriter out;
            out = new FileWriter(fileName);
            document.write(out);
            out.close();
        } catch (IOException ex) {
            System.out.println("Error! File can not be saved! Check the path to the file!");
        }
    }

    /**
     * loads all aviable connections from XML file
     * @return array of ConnectionProperties
     */
    
    public ArrayList<ConnectionProperties> loadConnections() {
        openXml();
        ArrayList<ConnectionProperties> conProps = new ArrayList<ConnectionProperties>();
        for (Iterator iterElem = root.elementIterator("connection"); iterElem.hasNext();) {
            Element conElem = (Element) iterElem.next();
            ConnectionProperties conProp = new ConnectionProperties();
            conProp.setName(conElem.attributeValue("name"));
            conProp.setDatabaseName(conElem.attributeValue("database"));
            conProp.setHost(conElem.attributeValue("host"));
            conProp.setPassword(conElem.attributeValue("password"));
            conProp.setUsername(conElem.attributeValue("username"));

            conProps.add(conProp);
        }
        return conProps;
    }

    /**
     * saves connection into XML file
     * @param host hostname:port string
     * @param database database string
     * @param username username string
     * @param password password string
     * @param name name of current config string
     * @throws java.io.IOException if problem with XML file occurs
     */
    
    public void saveConnection(String host, String database, String username, String password, String name) throws IOException {
        openXml();

        Element conEle = root.addElement("connection");
        conEle.addAttribute("name", name);
        conEle.addAttribute("host", host);
        conEle.addAttribute("database", database);
        conEle.addAttribute("username", username);
        conEle.addAttribute("password", password);
        root.addText("\n");

        saveXML();
    }

    /**
     * deletes connection from XML file by given name
     * @param name name of connection to be deleted
     */
    
    public void deleteConnection(String name) {
        for (Iterator iterElem = root.elementIterator("connection"); iterElem.hasNext();) {
            Element conElem = (Element) iterElem.next();
            if (conElem.attributeValue("name").equals(name)) {
                root.remove(conElem);
            }
        }
        saveXML();
    }
}
